package com.duoketech.service.web.member;
import com.duoketech.service.AbstractService;
import com.duoketech.model.member.Member;
import com.alibaba.druid.util.StringUtils;
import com.duoketech.exception.ValidateException;
import com.duoketech.utils.search.QueryConstructor;
import com.duoketech.utils.validator.Validator;
import com.duoketech.utils.validator.rules.string.StringLengthBetween;
import com.duoketech.utils.validator.rules.string.StringMustBeMobile;
import com.duoketech.utils.validator.rules.string.StringMustBePassWord;
import com.duoketech.utils.validator.rules.string.StringMustBeRequired;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Model;
public class MemberService extends AbstractService{
    public static MemberService ME = new MemberService();

    @Override
    public void validatorUpdate(Model<?> model, Kv kv) throws ValidateException {
        validatorAdd(model,kv);
    }

    @Override
    public void validatorAdd(Model<?> model, Kv kv) throws ValidateException {

        Validator validator = new Validator();
        validator.addRule(new StringMustBeRequired(model.getStr(Member.ID),"项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Member.ID),"项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(Member.UNIT_ID),"单位id项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Member.UNIT_ID),"单位id项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(Member.MOBILE),"手机号码项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Member.MOBILE),"手机号码项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(Member.PASSWORD),"密码项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Member.PASSWORD),"密码项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(Member.SALT),"指纹项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Member.SALT),"指纹项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(Member.NICK_NAME),"昵称项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Member.NICK_NAME),"昵称项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(Member.GENDER),"性别项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Member.GENDER),"性别项字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBeRequired(model.getStr(Member.AVATAR),"头像项为必填信息"));
        validator.addRule(new StringLengthBetween(model.getStr(Member.AVATAR),"头像项字符必须为1-50个字符",1,50));
        validator.excute();

    }

    @Override
    public void beforAdd(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterAdd(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforUpdate(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterUpdate(Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforQuery(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterQuery(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void beforDelete(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public void afterDelete(int id, Model<?> model, Kv kv) throws ValidateException {

    }

    @Override
    public String tableName() {
        return Member.dao.tableName();
    }

    @Override
    public void builderSQLExpected(Model<?> model, QueryConstructor queryConstruct, Kv kv) {

    }

    public void doResetPass(int memberId,String oldPass,String newPass,String confirmPass) throws ValidateException {
        Validator validator = new Validator();
        validator.addRule(new StringMustBePassWord(oldPass,"请输入正确的密码格式，密码只能是长度为6-16个字符数字和字母的组合,如a123456"));
        validator.addRule(new StringMustBePassWord(newPass,"请输入正确的密码格式，密码只能是长度为6-16个字符数字和字母的组合,如a123456"));
        validator.addRule(new StringMustBePassWord(confirmPass,"请输入正确的密码格式，密码只能是长度为6-16个字符数字和字母的组合,如a123456"));
        
        if(!newPass.equals(confirmPass)){
            throw new ValidateException("确认密码必须要和新密码一致");
        }
        validator.excute();

        Member member = Member.dao.findById(memberId);
        if(member==null){
            throw new ValidateException("当前记录不存在");
        }

        String salt = member.getStr(Member.SALT);
        oldPass = HashKit.md5(oldPass+salt);
        if(!oldPass.equals(member.getStr(Member.PASSWORD))){
            throw new ValidateException("登录密码不正确");
        }

        member.set(Member.PASSWORD,HashKit.md5(confirmPass+salt));
        member.update();
    }



    public Member doLogin(String userName,String pass) throws ValidateException {
        Validator validator = new Validator();
        validator.addRule(new StringMustBeRequired(userName,"登录用户名为必填信息"));
        validator.addRule(new StringLengthBetween(userName,"登录用户名字符必须为1-50个字符",1,50));
        validator.addRule(new StringMustBePassWord(pass,"请输入正确的密码格式，密码只能是长度为6-16个字符数字和字母的组合,如a123456"));
        validator.excute();

        Member member = queryMemberByMobile(userName);
        if(member==null){
            throw new ValidateException("当前记录不存在");
        }
        if(member.getInt(Member.STATUS)==Member.STATUS_LOCKED){
            throw new ValidateException("帐号被锁定，不允许登录操作");
        }

        if(member.getInt(Member.ROLE)<=Member.ROLE_COMMON){
            throw new ValidateException("你暂时没有权限访问当前系统，请联系管理员开启相关权限");
        }
        String salt = member.getStr(Member.SALT);
        pass = HashKit.md5(pass+salt);
        if(!pass.equals(member.getStr(Member.PASSWORD))){
            throw new ValidateException("用户名或者密码不正确....");
        }
        return member;
    }

    public Member queryMemberByMobile(String userName) {
        String sql="select * from "+Member.dao.tableName()+" where mobile=?  limit 1";
        return Member.dao.findFirst(sql,userName);
    }


    public Member queryMemberByOpenId(String openId){
        String sql="select * from "+Member.dao.tableName()+" where open_id=? limit 1";
        return Member.dao.findFirst(sql,openId);
    }

    public void bindMember(int memberId,String mobile,String passWord,String confirmPassWord,String code) throws ValidateException {
        Validator validator = new Validator();
        validator.addRule(new StringLengthBetween(mobile,"请输入正确的手机号码",1,11));
        validator.addRule(new StringMustBeRequired(passWord,"请输入用户密码"));
        validator.addRule(new StringMustBeRequired(confirmPassWord,"请输入用户确认密码"));
//        validator.addRule(new StringMustBeRequired(code,"请输入验证码"));
        validator.addRule(new StringMustBePassWord(passWord,"用户密码必须是字母及数字的组合且长度为6-16位"));
        validator.addRule(new StringMustBePassWord(confirmPassWord,"确认密码必须是字母及数字的组合且长度为6-16位"));
        validator.excute();

        Member temp = queryMemberByMobile(mobile);
        if(temp!=null){
            throw new ValidateException("当前手机号码被绑定了！！");
        }
        String salt = HashKit.generateSaltForSha256();
        passWord = HashKit.md5(passWord+salt);
        Member member = new Member();
        member.set(Member.ID,memberId);
        member.set(Member.MOBILE,mobile);
        member.set(Member.PASSWORD,passWord);
        member.set(Member.SALT,salt);
        member.update();
    }


    public Member wxDoLogin(String mobile,String passWord) throws ValidateException {

        Validator validator = new Validator();
        validator.addRule(new StringMustBeRequired(mobile,"请输入用户帐号"));
        validator.addRule(new StringLengthBetween(mobile,"请输入正确的手机号码格式",1,11));
        validator.addRule(new StringMustBePassWord(passWord,"密码必须是字母及数字的组合且长度为6-16位"));
        validator.excute();

        String sql="select * from "+Member.dao.tableName()+" where mobile = ? limit 1";
        Member member =  Member.dao.findFirst(sql,mobile);
        if(member==null){
            throw new ValidateException("帐号或者密码不正确");
        }

        if(member.getInt(Member.STATUS)==Member.STATUS_LOCKED){
            throw new ValidateException("帐号被锁定，不允许登录操作");
        }

        String salt = member.getStr(Member.SALT);
        String newPass = HashKit.md5(passWord+salt);
        if(!member.getStr(Member.PASSWORD).equals(newPass)){
            throw new ValidateException("帐号或者密码不正确");
        }
        return member;
    }

    public Member queryMemberBy(String memberSer){
    	String sql="select * from "+Member.dao.tableName()+" where member_ser=? limit 1";
    	return Member.dao.findFirst(sql,memberSer);
    }
    
    public void doBindShopAccout(String mobile,String pass,String newPass,String memberSer) throws ValidateException{
    	 Validator validator = new Validator();
    	 validator.addRule(new StringMustBeRequired(mobile,"请输入用户商户ID"));
         validator.addRule(new StringLengthBetween(mobile,"请输入正确的用户商户ID",1,11));
         validator.addRule(new StringMustBeRequired(mobile,"请输入用户帐号"));
         validator.addRule(new StringLengthBetween(mobile,"请输入正确的手机号码格式",1,11));
         validator.addRule(new StringMustBePassWord(pass," 设定密码必须是字母及数字的组合且长度为6-16位"));
         validator.addRule(new StringMustBePassWord(newPass,"确认密码必须是字母及数字的组合且长度为6-16位"));
         validator.excute();
    	
         if(!pass.equals(newPass)){
        	 throw new ValidateException("新密码和确认密码必须一致");
         }
    	
    	Member member = queryMemberBy(memberSer);
    	if(member==null){
    		throw new ValidateException("当前商户会员ID不存在");
    	}
    	
//    	if(!StringUtils.isEmpty(member.getStr(Member.SALT))){
//    		throw new ValidateException("当前商户会员ID已经被设置过了，无法重复操作");
//    	}
    	String salt = HashKit.generateSaltForSha256();
    	pass = HashKit.md5(pass+salt);
    	member.set(Member.MOBILE, mobile);
    	member.set(Member.PASSWORD, pass);
    	member.set(Member.SALT, salt);
    	member.update();
    	
    }
}